package com.ad2campaign.engine.core.handlers;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import com.ad2campaign.engine.utils.BootOperation;
import com.ad2campaign.engine.web.jetty.EngineHttpServer;

public class OPSHandler implements IoHandler
{

	private EngineHttpServer server;
	 
	public OPSHandler(EngineHttpServer server) {
		super();
		this.server = server;
	
	}

	@Override
	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void messageReceived(IoSession ioSession, Object object) throws Exception 
	{
		String command=object.toString();
 		if (BootOperation.SHUTDOWN.getOperation().equalsIgnoreCase(command))
		{
  			
 			System.out.println("*******************************RECIEVED SHUTDOWN TRAP*****************************");
 			System.out.println("*                               STOPPING ENGINE                                  *");
 			System.out.println("*                ENGINE WILL EXITING NOW [INVOKING SYSTEM.EXIT(0)]               *");
 			System.out.println("**********************************************************************************");
			server.stop();
 			System.out.println("-----------------------[Shutdown completed gracefully]----------------------------");
 			System.exit(0);
  		}
  	}

	@Override
	public void messageSent(IoSession arg0, Object arg1) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionClosed(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionCreated(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionOpened(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
